package com.atguigu.ucenter.controller;


import com.atguigu.commonUtils.JwtUtils;
import com.atguigu.commonUtils.R;
import com.atguigu.commonUtils.orderVo.MemberOrder;
import com.atguigu.ucenter.entity.Member;
import com.atguigu.ucenter.entity.vo.MemberVo;
import com.atguigu.ucenter.entity.vo.RegisterVo;
import com.atguigu.ucenter.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static org.bouncycastle.asn1.x500.style.RFC4519Style.c;
import static org.bouncycastle.asn1.x500.style.RFC4519Style.member;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author Rayyy666
 * @since 2022-04-14
 */
@RestController
@RequestMapping("/ucenter/member")
//@CrossOrigin
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 会员登录
     *
     * @param member
     * @return
     */
    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody Member member) {

        //返回token值，使用jwt实现
        String token = memberService.login(member);

        return R.ok().data("token", token);
    }

    /**
     * 会员注册
     *
     * @param registerVo
     * @return
     */
    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        R result = memberService.register(registerVo);
        return R.ok().data("result", result);
    }

    /**
     * 根据token获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法，根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        Member member = memberService.getById(memberId);
        System.out.println(member);
        return R.ok().data("member", member);
    }

    /**
     * 根据用户ID 查询用户的信息
     *
     * @param memberId
     * @return
     */
    @GetMapping("getMemberInfoById/{memberId}")
    public MemberVo getMemberInfoById(@PathVariable String memberId) {
        Member member = memberService.getById(memberId);
        MemberVo memberVo = new MemberVo();
        BeanUtils.copyProperties(member, memberVo);
        return memberVo;
    }

    /**
     * 根据用户id 返回用户信息 用于订单
     *
     * @param id
     * @return
     */
    @PostMapping("getUserInfoOrder/{id}")
    public MemberOrder getUserInfoOrder(@PathVariable String id) {
        Member member = memberService.getById(id);
        MemberOrder memberOrder = new MemberOrder();

        BeanUtils.copyProperties(member, memberOrder);
        return memberOrder;
    }

    /**
     * 查询某一天的注册人数
     *
     * @param day
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterOneDay(day);
        return R.ok().data("countRegister", count);

    }
}

